package com.cloudcross.ssp.web.operator.main.agent;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloudcross.ssp.base.web.SimpleController;
import com.cloudcross.ssp.common.consts.Status;
import com.cloudcross.ssp.common.utils.LangUtil;
import com.cloudcross.ssp.common.utils.Md5Tool;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.model.Account;
import com.cloudcross.ssp.model.OperatorAgentDetail;
import com.cloudcross.ssp.service.IOperatorAgentAccountService;
import com.cloudcross.ssp.service.IResourcesService;

@Controller
@RequestMapping("/operator/main/agent/operator-agent-account")
public class OperatorAgentAccountController extends SimpleController {

    @Autowired
    protected IOperatorAgentAccountService operatorAgentAccountService;

    @Autowired
    protected IResourcesService            resourceService;

    @RequestMapping("/list")
    public String list(Model model, @RequestParam HashMap<String, Object> paramMap,
                       @RequestParam(defaultValue = "1") int page) {
        paramMap.put("operatorId", getLoginUser().getOperatorId());
        int totalRow = operatorAgentAccountService.countByParams(paramMap);
        Pager pager = new Pager();
        pager.setPage(page);
        pager.setTotalRow(totalRow);
        List<OperatorAgentDetail> operatorAgentDetails = operatorAgentAccountService.findByParams(paramMap, pager);
        model.addAllAttributes(paramMap);
        model.addAttribute("pager", pager);
        model.addAttribute("operatorAgentDetailList", operatorAgentDetails);
        return page("list");
    }

    @RequestMapping("/create")
    public String create() {
        return page("create");
    }

    @RequestMapping("/edit/{id}")
    public String edit(Model model, @PathVariable long id) {
        OperatorAgentDetail operatorAgentDetail = operatorAgentAccountService.findById(id);
        List<Long> auth = operatorAgentAccountService.findOperatorAgentZoneAuth(operatorAgentDetail.getAccountId());
        model.addAttribute("operatorAgentDetail", operatorAgentDetail);
        model.addAttribute("zoneRight", auth);
        return page("edit");
    }

    @RequestMapping("save")
    public String save(Model model, Long[] zoneRight, @ModelAttribute("form") OperatorAgentDetail t) {
        Account account = getLoginUser();
        t.setIconPath(account.getIconPathurl());
        t.setLonginUrl(account.getLonginUrl());
        if (t.getAccountId() == null) {//新建运营商代理
            t.setOperatorId(account.getOperatorId());
            t.setPassword(Md5Tool.getMd5(t.getPassword()));
            operatorAgentAccountService.addOperatorAgentDedail(t);
            operatorAgentAccountService.add(t);
            if (zoneRight != null) {
                List<Long> right = new ArrayList<Long>(Arrays.asList(zoneRight));
                right.add(new Long(162));
                resourceService.addAccountResourcess(t.getAccountId(), right);
            } else {
                List<Long> right = new ArrayList<Long>();
                right.add(new Long(162));
                resourceService.addAccountResourcess(t.getAccountId(), right);
            }
        } else {//编辑保存运营商代理
            if (t.getPassword() != null) {
                t.setPassword(Md5Tool.getMd5(t.getPassword()));
            } else {
                t.setPassword(null);
            }
            operatorAgentAccountService.editOperatorAgentDetail(t);
            operatorAgentAccountService.edit(t);

            if (zoneRight != null) {
                List<Long> right = new ArrayList<Long>(Arrays.asList(zoneRight));
                right.add(new Long(162));
                resourceService.addAccountResourcess(t.getAccountId(), right);
            } else {
                List<Long> right = new ArrayList<Long>();
                right.add(new Long(162));
                resourceService.addAccountResourcess(t.getAccountId(), right);
            }
        }
        return redirect(page("list"));
    }

    @RequestMapping("enable")
    public @ResponseBody String enable(Long[] id) {
        if (ArrayUtils.isNotEmpty(id)) {
            operatorAgentAccountService.updateStatus(LangUtil.array2List(id), Status.disable.value);
            List<Long> operatorAgentIdList = operatorAgentAccountService.findOperatorAgentId(LangUtil.array2List(id));
            operatorAgentAccountService.updateOperatorAgentStatus(operatorAgentIdList, Status.enable.value);
        }
        return OK;
    }

    @RequestMapping("disable")
    public @ResponseBody String disable(Long[] id) {
        if (!ArrayUtils.isEmpty(id)) {
            operatorAgentAccountService.updateStatus(LangUtil.array2List(id), Status.enable.value);
            List<Long> operatorAgentIdList = operatorAgentAccountService.findOperatorAgentId(LangUtil.array2List(id));
            operatorAgentAccountService.updateOperatorAgentStatus(operatorAgentIdList, Status.disable.value);
        }
        return OK;
    }

    @RequestMapping("delete")
    public @ResponseBody String delete(Long[] id) {
        if (!ArrayUtils.isEmpty(id)) {
            operatorAgentAccountService.deleteOperator(LangUtil.array2List(id));
            List<Long> operatorAgentIdList = operatorAgentAccountService.findOperatorAgentId(LangUtil.array2List(id));
            operatorAgentAccountService.updateOperatorAgentStatus(operatorAgentIdList, Status.delete.value);
        }
        return OK;
    }
}
